package com.boarsoft.boar.soagov.face.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.BaseConfig;
import com.boarsoft.boar.dir.DirBiz;
import com.boarsoft.boar.dir.bean.DirTreeNode;
import com.boarsoft.boar.entity.DirInfo;
import com.boarsoft.boar.soagov.entity.FaceInfo;
import com.boarsoft.boar.soagov.face.FaceBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;

@RestController
@RequestMapping("/face")
public class FaceAction {
	@Autowired
	private DirBiz dirBiz;
	@Autowired
	private FaceBiz faceBiz;

	/**
	 * 查询所有接口
	 * 
	 * @return
	 */
	@RequestMapping("/list.do")
	// @Authorized(code = "face.list")
	public ReplyInfo<Object> list(String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<FaceInfo> pr = faceBiz.list(key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, pr);
	}

	/**
	 * 保存（新增或修改）接口
	 * 
	 * @param parent
	 * @param c
	 *            faceInfo
	 * @return
	 */
	@RequestMapping("/save.do")
	// @Authorized(code = "face.save")
	public ReplyInfo<Object> save(String parent, FaceInfo c) {
		if (Util.strIsEmpty(c.getId())) {
			return this.add(parent, c);
		}
		return this.update(c);
	}

	/**
	 * 新增接口
	 * 
	 * @param parent
	 * @param c
	 *            faceInfo
	 * @return
	 */
	@RequestMapping("/add.do")
	// @Authorized(code = "face.save")
	public ReplyInfo<Object> add(String parent, FaceInfo c) {
		// 检查parent是否存在，取出path拼接
		DirInfo d = new DirInfo();
		d.setParentId(parent);
		// 设置这个目录在目录树上的路径
		if (BaseConfig.DIR_ROOT.equals(parent)) {
			d.setPath("/");
			d.setLevel(1);
		} else {
			DirInfo p = dirBiz.get(parent);
			d.setLevel(p.getLevel() + 1);
			if ("/".equals(p.getPath())) {
				d.setPath(String.format("%s%s", p.getPath(), p.getId()));
			} else {
				d.setPath(String.format("%s/%s", p.getPath(), p.getId()));
			}
		}
		//
		// o.setId(id); // id是自动生成的
		d.setCode(c.getCode());
		d.setName(c.getName());
		// o.setLevel(level); // level是在add当中+1的
		// o.setChildCount(childCount); //初始值已为0
		d.setType(DirInfo.TYPE_FACE);
		//

		ReplyInfo<Object> ro = faceBiz.add(d, c);
		if (ro.isSuccess()) {
			DirTreeNode dtn = new DirTreeNode((DirInfo) ro.getData());
			return new ReplyInfo<Object>(true, dtn);
		}
		return new ReplyInfo<Object>(ro.getData());
	}

	/**
	 * 修改接口
	 * 
	 * @param o
	 * @return
	 */
	@RequestMapping("/update.do")
	// @Authorized(code = "face.update")
	public ReplyInfo<Object> update(FaceInfo o) {
		// 只能改DirInfo
		DirInfo d = dirBiz.get(o.getId());
		// d.setCode(code);
		d.setName(o.getName());
		// d.setDesc(desc);
		// d.setPath(path); //path是由code决定
		//
		FaceInfo c = faceBiz.get(o.getId());
		// c.setAppId(appId);
		c.setCode(o.getCode());
		c.setName(o.getName());
		c.setFace(o.getFace());
		c.setVer(o.getVer());
		c.setStatus(o.getStatus());
		c.setMemo(o.getMemo());
		// update方法需判断code是否改变，一旦改变，需修改子节点的path
		ReplyInfo<Object> ro = faceBiz.update(d, c);
		if (ro.isSuccess()) {
			DirTreeNode dtn = new DirTreeNode((DirInfo) ro.getData());
			return new ReplyInfo<Object>(true, dtn);
		}
		return ro;
	}

	/**
	 * 删除接口
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete.do")
	// @Authorized(code = "face.delete")
	public ReplyInfo<Object> delete(String id) {
		faceBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 获取单个接口信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/get.do")
	// @Authorized(code = "face.get")
	public ReplyInfo<Object> get(String id) {
		FaceInfo a = faceBiz.get(id);
		return new ReplyInfo<Object>(true, a);
	}
}